<template>
  <el-container class="el-home">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主题区域 -->
    <el-container>
      <!-- 侧边栏   （双重for循环渲染 ）-->
      <el-aside :width="isCollase ? '64px' : '200px'">
        <!-- 侧边栏折叠与否 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          unique-opened
          :collapse="isCollase"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id" v-for="item in meanList" :key="item.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item v-for="subItem in item.children" :key="subItem.id" @click="saveNavState(subItem.id)">
              <i class="el-icon-menu"></i>
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主题内容 -->
      <el-main><router-view></router-view></el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // meanList: [],
      meanList: [
        {
          id: '1',
          authName: '用户管理',
          children: [{ id: '1 - 1', authName: '用户列表' }],
        },
        {
          id: '2',
          authName: '权限管理',
          children: [
            { id: '2 - 1', authName: '角色列表' },
            { id: 2 - 1, authName: '权限列表' },
          ],
        },
        {
          id: '3',
          authName: '商品管理',
          children: [
            { id: '3-1', authName: '商品列表' },
            { id: '3-2', authName: '分类参数' },
            { id: '3-3', authName: '商品分类' },
          ],
        },
      ],
      // 是否折叠
      isCollase: false,
      // 激活链接地址
      activePath: ''
    }
  },
  created() {
    this.getMenList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    //  获取所有侧边栏菜单
    async getMenList() {
      // const { data: res } = await this.$http.get('menu')
      // if (res.meta.status !== 200) return this.$message.error(res.meta)
      // this.menulist = res.data
      // console.log('res')
    },
    // 侧边栏是否折叠
    toggleCollapse() {
      // console.log('判断')
      this.isCollase = !this.isCollase
    },
    // 保存激活的链接
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
    },
  },
}
</script>

<style scoped>
.el-home {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
}
.el-header div {
  display: flex;
  align-items: center;
}
.el-header div span {
  margin-left: 15px;
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.el-menu {
  border-right: none;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
}
</style>
